package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.CompanyInvoiceEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.InvoiceRateEntity;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 杨畔
 * @date: 2020/12/29 18:14
 * @Description:
 */
public class CargoPublishPayAgreementViewModel extends AndroidViewModel {

    public MutableLiveData<List<GoodsInfoTypeEntity3>> ldPay = new MutableLiveData<>();
    public MutableLiveData<List<GoodsInfoTypeEntity3>> ldInvoice = new MutableLiveData<>();
    public MutableLiveData<List<CompanyInvoiceEntity>> ldCompanyInvoice = new MutableLiveData<>();
    public MutableLiveData<List<InvoiceRateEntity>> ldInvoiceRateEntity = new MutableLiveData<>();

    public CargoPublishPayAgreementViewModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 获取支付方式
     */
    public void getPayType() {
        List<GoodsInfoTypeEntity3> list = new ArrayList<>();
        list.add(new GoodsInfoTypeEntity3("0", "付款拉货"));
        list.add(new GoodsInfoTypeEntity3("1", "货到付款"));
        list.add(new GoodsInfoTypeEntity3("2", "月底结算"));
        ldPay.postValue(list);
    }

    /**
     * 获取发票要求
     */
    public void getInvoiceType() {
        List<GoodsInfoTypeEntity3> list = new ArrayList<>();
        list.add(new GoodsInfoTypeEntity3("1", "不开发票"));
        list.add(new GoodsInfoTypeEntity3("2", "普通发票"));
        list.add(new GoodsInfoTypeEntity3("3", "增值税专用发票"));
        ldInvoice.postValue(list);
    }

    /**
     * 获取公司发票列表
     */
    public void getInvoiceList(Context context) {
        //判断公司或者个人调用不同的发票接口
        DialogHelper.showProgressDialog(context, "请求中...");
        if (UserOperating.getInstance().isCompany()) {
            NewAPIManager.getInstance().getLogisticsAPI().getCompanyInvoiceList().enqueue(invoiceCallBack());
        } else {
            NewAPIManager.getInstance().getLogisticsAPI().getPersonInvoiceList().enqueue(invoiceCallBack());
        }
    }

    /**
     * 获取发票列表信息
     *
     * @return
     */
    public void getInvoices(Context context) {
        DialogHelper.showProgressDialog(context, "请求中...");
        Map<String, Object> map = new HashMap<>();
        APIManager.getInstance().getDriverAPI().getInvoiceListInfo(map).enqueue(new ICallBack<BaseEntity<List<InvoiceRateEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<InvoiceRateEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data!=null&&data.getData()!=null){
                    ldInvoiceRateEntity.postValue(data.getData());
                }
            }
        });
    }

    @NotNull
    private ICallBack<BaseEntity<List<CompanyInvoiceEntity>>> invoiceCallBack() {
        return new ICallBack<BaseEntity<List<CompanyInvoiceEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CompanyInvoiceEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() != null) {
                    ldCompanyInvoice.postValue(data.getData());
                }
            }
        };
    }
}
